﻿Option Explicit On
Option Strict On

Imports System.Net
Imports System.Net.Mail

Public Class clsEmail

    ''' <summary>
    ''' Email content structure
    ''' </summary>
    ''' <remarks></remarks>
    Public Structure stMailDetail

        Dim strFromAdress As String             'from email address
        Dim strToAddress As String              'to email address
        Dim lstCcAddress As List(Of String)     'list of cc
        Dim lstBccAddress As List(Of String)    'list of bcc
        Dim strSubject As String                'subject
        Dim strBody As String                   'body

        Dim strSmtpHost As String               'smtp host (gmail: smtp.gmail.com)
        Dim intSmtpPort As Integer              'smtp port (gmail: 587)
        Dim blnSSL As Boolean                   'use ssl connection
        Dim strSmtpUser As String               'email login
        Dim strSmtpPass As String               'email password

    End Structure

    ''' <summary>
    ''' Constructor
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub New()

    End Sub

    ''' <summary>
    ''' Send email
    ''' </summary>
    ''' <param name="stEmail"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function fncSendEmail(ByVal stEmail As stMailDetail) As Boolean

        fncSendEmail = False

        Dim objMail As MailMessage = Nothing
        Dim objSmtp As SmtpClient = Nothing

        Try
            objMail = New MailMessage()
            objSmtp = New SmtpClient()

            'from - to
            objMail.From = New MailAddress(stEmail.strFromAdress)
            objMail.To.Add(New MailAddress(stEmail.strToAddress))

            'cc
            If stEmail.lstCcAddress IsNot Nothing Then
                For i As Integer = 0 To stEmail.lstCcAddress.Count - 1
                    objMail.CC.Add(New MailAddress(stEmail.lstCcAddress(i)))
                Next
            End If

            'bcc
            If stEmail.lstBccAddress IsNot Nothing Then
                For i As Integer = 0 To stEmail.lstBccAddress.Count - 1
                    objMail.Bcc.Add(New MailAddress(stEmail.lstBccAddress(i)))
                Next
            End If

            'subject - content
            objMail.Subject = stEmail.strSubject
            objMail.Body = stEmail.strBody

            'Smtp config
            objSmtp.Host = stEmail.strSmtpHost
            objSmtp.Port = stEmail.intSmtpPort
            objSmtp.Timeout = 30000 '30s
            objSmtp.EnableSsl = stEmail.blnSSL
            objSmtp.DeliveryMethod = SmtpDeliveryMethod.Network
            objSmtp.UseDefaultCredentials = False
            objSmtp.Credentials = New NetworkCredential(stEmail.strSmtpUser, stEmail.strSmtpPass)

            Try
                objSmtp.Send(objMail)
            Catch ex As Exception
                Return False
            End Try

            Return True

        Catch ex As Exception
            Return False
        Finally
            If objMail IsNot Nothing Then objMail.Dispose()
            objMail = Nothing
            objSmtp = Nothing
        End Try

    End Function

End Class
